﻿using System;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Admin.Categories
{
    public partial class Create : BasePage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "")) {
                return;
            }

            GetDepartments();
        }

        private void GetDepartments() {
            var departments = DepartmentRepository.QueryOver().OrderBy(d => d.DepartmentName).Asc.List();
            BindDropDownList(DepartmentList, departments, "DepartmentName", "Id", "--Select--", "0");
        }

        protected void CreateNewCategoryClick(object sender, EventArgs e) {
            ErrorMsg.Text = "";
            var category = new EnquiryCategory
                {
                    Department = DepartmentRepository.LoadReference(int.Parse(DepartmentList.SelectedItem.Value)),
                    CategoryName = CategoryName.Text,
                    AvailableForSelection = true
                };

            if (!IsValidObject(category, ErrorMsg)) {
                return;
            }

            EnquiryCategoryRepository.Add(category);
            Response.Redirect("~/Admin/Categories/List.aspx#" + category.Id);
        }
    }
}